package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewCpostGradeSaveCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCpostGradeMapper;
import com.jackrain.nea.fc.utils.UserInformation;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.security.Security4Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 组织信息-服务开发
 *
 * @author xuxinshuai
 * @create 2017/12/15
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ViewCpostGradeSaveCmdImpl extends CommandAdapter implements ViewCpostGradeSaveCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCpostGradeSaveCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0||param.getJSONArray("data").size()<=0) {
            throw new NDSException("参数格式错误！");
        }
//        log.debug("-----------传入等级参数----------" + param);
        ViewCpostGradeMapper viewCpostGradeMapper = ApplicationContextHandle.getBean(ViewCpostGradeMapper.class);
        JSONArray paramArray = param.getJSONArray("data");
        User user = Security4Utils.getUser(UserInformation.USER_NAME);
        JSONArray errorArray = new JSONArray();
        for (int i = 0; i < paramArray.size(); i++) {

            try {

                JSONObject paramObj = paramArray.getJSONObject(i);
                Integer gradeCount = viewCpostGradeMapper.selectGradeCount(paramObj.getString("HR007_ID"));
                if (gradeCount > 0) {

                    JSONObject updateObj = new JSONObject();
                    updateObj.put("HR007_ID", paramObj.getString("HR007_ID"));
                    updateObj.put("ECODE", paramObj.getString("ECODE"));
                    updateObj.put("ENAME", paramObj.getString("ENAME"));
                    updateObj.put("MODIFIEDDATE", paramObj.getString("MODIFIEDDATE"));
                    updateObj.put("MODIFIERNAME", user.getName());
                    updateObj.put("MODIFIERENAME", user.getEname());
                    updateObj.put("MODIFIERID", Integer.valueOf(user.getId()).longValue());
                    int updateRes = viewCpostGradeMapper.updatePostGrade(updateObj);
                    if (updateRes <=0) {
                        throw new NDSException("更新失败HR007_ID:" + paramObj.getString("HR007_ID"));
                    }

                } else {
                    JSONObject insertObj = new JSONObject();
                    Long postGradeid = Tools.getSequence("CP_C_POSTGRADE");
                    insertObj.put("ID", postGradeid);
                    insertObj.put("AD_CLIENT_ID", Integer.valueOf(user.getClientId()).longValue());
                    insertObj.put("AD_ORG_ID", Integer.valueOf(user.getOrgId()).longValue());
                    insertObj.put("HR007_ID", paramObj.getString("HR007_ID"));
                    insertObj.put("ECODE", paramObj.getString("ECODE"));
                    insertObj.put("ENAME", paramObj.getString("ENAME"));
                    insertObj.put("MODIFIEDDATE", paramObj.getString("MODIFIEDDATE"));
                    insertObj.put("CREATIONDATE", paramObj.getString("MODIFIEDDATE"));
                    insertObj.put("MODIFIERNAME", user.getName());
                    insertObj.put("MODIFIERENAME", user.getEname());
                    insertObj.put("MODIFIERID", Integer.valueOf(user.getId()).longValue());
                    insertObj.put("OWNERID", Integer.valueOf(user.getId()).longValue());
                    insertObj.put("OWNERNAME", user.getName());
                    insertObj.put("OWNERENAME", user.getEname());
                    int insertRes = viewCpostGradeMapper.insertPostGrade(insertObj);
                    if (insertRes <= 0) {
                        throw new NDSException("新增失败");
                    }
                }
            } catch (NDSException e) {
                JSONObject errObj = new JSONObject();
                errObj.put("ErrorMessage", e.getSimpleMessage());
                errorArray.add(errObj);
            }
        }

        if (errorArray.size() > 0) {
            vh.put("error", errorArray);
            vh.put("code", -1);
        } else {
            vh.put("code", 0);
        }
        return vh;
    }
}





